package com.jxzn.locust.config;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.MethodParameter;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.method.support.ModelAndViewContainer;
import org.springframework.web.servlet.mvc.method.annotation.ViewNameMethodReturnValueHandler;

import freemarker.ext.beans.BeansWrapper;
import freemarker.ext.beans.BeansWrapperBuilder;
import freemarker.template.Configuration;

public class CommonReturnInterceptorHandler extends ViewNameMethodReturnValueHandler {

    Logger logger = LoggerFactory.getLogger(getClass());

    @Override
    public boolean supportsReturnType(MethodParameter returnType) {
        return returnType.getParameterType() == String.class;
    }

    @Override
    public void handleReturnValue(Object returnValue, MethodParameter returnType, ModelAndViewContainer mavContainer,
            NativeWebRequest webRequest) throws Exception {
        if (null == mavContainer) {
            return;
        }
        BeansWrapper bw = new BeansWrapperBuilder(Configuration.VERSION_2_3_28).build();
        mavContainer.addAttribute("enums", bw.getEnumModels());
        super.handleReturnValue(returnValue, returnType, mavContainer, webRequest);
    }

}
